﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization.Json;
using System.IO;

namespace HHG.Common
{
    public class Json
    {
        public static string DataTable2Json(System.Data.DataTable dt, bool withTableName)
        {
            System.Text.StringBuilder jsonBuilder = new System.Text.StringBuilder();
            if (withTableName)
            {
                jsonBuilder.Append("{\"");
                jsonBuilder.Append(dt.TableName);
                jsonBuilder.Append("\":");
            }
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            if (withTableName) jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }

        //dataset转json  
        public static string DataTable2Json2(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder jsonBuilder = new StringBuilder();
                jsonBuilder.Append("{");
                jsonBuilder.Append(dt.TableName);
                jsonBuilder.Append(":[");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //jsonBuilder.Append("\"");
                        jsonBuilder.Append(dt.Columns[j].ColumnName);
                        jsonBuilder.Append(":'");
                        jsonBuilder.Append(dt.Rows[i][j].ToString());
                        jsonBuilder.Append("',");
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("},");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]");
                jsonBuilder.Append("}");
                return jsonBuilder.ToString();
            }
            else
            {
                return "{ds:{}}";
            }
        }


        public static string DataTable2Json(DataTable dt,int RecoderCount)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder jsonBuilder = new StringBuilder();
                jsonBuilder.Append("{");
                jsonBuilder.Append("\"total\":" + RecoderCount + ",");
                jsonBuilder.Append("\"rows\":[" );
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    jsonBuilder.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        //jsonBuilder.Append("\"");
                        jsonBuilder.Append("\"" + dt.Columns[j].ColumnName + "\"");
                        jsonBuilder.Append(":\"");
                        jsonBuilder.Append(dt.Rows[i][j].ToString());
                        jsonBuilder.Append("\",");
                    }
                    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                    jsonBuilder.Append("},");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("]");
                jsonBuilder.Append("}");
                return jsonBuilder.ToString();
            }
            else
            {
                return "{ds:{}}";
            }
        }


        /// <summary>

        /// 生成Json格式

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="obj"></param>

        /// <returns></returns>

        public static string GetJson<T>(T obj)
        {

            DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());

            using (MemoryStream stream = new MemoryStream())
            {

                json.WriteObject(stream, obj);

                string szJson = Encoding.UTF8.GetString(stream.ToArray()); return szJson;

            }

        }

        /// <summary>

        /// 获取Json的Model

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="szJson"></param>

        /// <returns></returns>

        public static T ParseFromJson<T>(string szJson)
        {

            T obj = Activator.CreateInstance<T>();

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
            {

                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());

                return (T)serializer.ReadObject(ms);

            }

        }
    }
}
